namespace MockingBird.Simulator.Framework.Persistence
{
    using System;
    using System.IO;
    using System.Text;
    using System.Xml;
    using MockingBird.Common.Infrastructure.DependencyResolution;
    using MockingBird.Common.Infrastructure.Logging;
    using MockingBird.Simulator.Framework.Configuration;
    using MockingBird.Simulator.Framework.Interfaces;

    public abstract class MessageFileLoggerBase : IMessageLogger
    {
        protected ILogger logger;
        protected IExecutionContext context;
        protected string workingFolder;
        protected string workingFileName;
        protected XmlDocument message;
        protected string messageName;

        public ApplicationConfiguration AppConfig { get; set; }

        public MessageFileLoggerBase(ILogger logger)
        {
            this.logger = logger;
        }

        /// <summary>
        /// Logs the message to a file.
        /// </summary>
        /// <param name="message"></param>
        public void LogMessage(IExecutionContext executionContext, System.Xml.XmlDocument messageToLog)
        {
            this.context = executionContext;
            this.message = messageToLog;
            this.InitAppConfiguration();
            this.SetWorkingFolder();
            this.CreateWorkingFolder();
            this.SetWorkingFileName();
            this.PersistMessage();
        }

        /// <summary>
        /// Sets the working folder for the logger.
        /// </summary>
        protected abstract void SetWorkingFolder();



        /// <summary>
        /// Sets the working file name for the logger.
        /// </summary>
        protected abstract void SetWorkingFileName();

        protected void BuildWorkingFolder(string messageCategory)
        {
            string currentEndpointFolder = Path.Combine(this.AppConfig.EndpointsRoot, this.context.EndpointName);
            string logRoot = Path.Combine(currentEndpointFolder, "logs");

            // Get the file path

            this.workingFolder = Path.Combine(logRoot, messageCategory);
            this.workingFolder = Path.Combine(this.workingFolder, this.context.EndpointName);
        }

        protected void BuildWorkingFileName(string prefix)
        {
            string suffix = DateTime.Now.ToString("yyyyMMdd_hhmmssfff");

            string fileName = string.Format("{0}_{1}_{2}.{3}", this.context.EndpointName, suffix, Guid.NewGuid().ToString(), "xml");

            this.workingFileName = Path.Combine(this.workingFolder, fileName);

        }

        private void InitAppConfiguration()
        {
            if (this.AppConfig == null)
            {
                AppConfig = DependencyResolver.DefaultContainer.Resolve<ApplicationConfiguration>();
            }
        }



        /// <summary>
        /// Creates the working folder if one does not exist already.
        /// </summary>
        private void CreateWorkingFolder()
        {
            if (!Directory.Exists(this.workingFolder))
            {
                Directory.CreateDirectory(this.workingFolder);
            }
        }

        /// <summary>
        /// Writes the message to the file.
        /// </summary>
        private void PersistMessage()
        {
            this.logger.Log("MessageFileLoggerBase : PersistMessage()");
            XmlTextWriter xtw = new XmlTextWriter(this.workingFileName, Encoding.UTF8);
            this.message.WriteTo(xtw);
            xtw.Close();
        }
    }
}